package org.jeecg.common.util;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention ; 
import java.lang.annotation.RetentionPolicy;
import java.math.BigDecimal; 
import java.lang.reflect.Field;
/** 
* @author 作者 : xzs
* @version 创建时间：2021-6-1 13:59:36 
*/
public class DefaultType {

	@Retention(RetentionPolicy.RUNTIME)
	 public @interface DefaultString {
	 String value(); 
	}
	
	@Retention(RetentionPolicy.RUNTIME)
	 public @interface DefaultInteger  {
	 int value(); 
	}
	
	
	public void setDefaults() {
        for (Field f : getClass().getFields()) {
            f.setAccessible(true);
            try {
                if (f.get(this) == null) {
                    f.set(this, getDefaultValueFromAnnotation(f.getAnnotations()));
                }
            } catch (IllegalAccessException e) { // shouldn't happen because I used setAccessible
            }
        }
    }

    private Object getDefaultValueFromAnnotation(Annotation[] annotations) {
        for (Annotation a : annotations) {
            if (a instanceof DefaultString)
                return ((DefaultString)a).value();
            if (a instanceof DefaultInteger)
                return ((DefaultInteger)a).value();
        }
        return null;
    }
}
